using System;
using System.Configuration;

namespace Cfg
{
    public static class ConnectionStrings
    {
        /// <summary>
        /// Safely get connection string by name.
        /// If connection string does not exist null is returned.
        /// </summary>
        /// <param name="key">Connection string name</param>
        /// <param name="throwException">if set true, <see cref="ArgumentNullException"/> will be thrown if conneciton string does not exist</param>
        /// <returns>Connection string</returns>
        public static string Get(string key, bool throwException = false)
        {
            var connectionStringSettings = ConfigurationManager.ConnectionStrings[key];
            if (connectionStringSettings == null)
            {
                if (throwException)
                    throw new ArgumentNullException("connectionStringSettings");

                return null;
            }

            return connectionStringSettings.ConnectionString;
        }
    }
}